package org.iabako.client.ui.page.importdata;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.Panel;
import org.iabako.client.tools.IabakoAsyncCallback;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.iservice.importdata.ImportClientServiceAsync;

import java.util.Date;

/**
 * Created by jose on 17/02/15.
 */
public class ImportClient extends Composite {

    interface MyUiBinder extends UiBinder<Panel, ImportClient> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    @UiField
    FormPanel fileUploadForm;
    @UiField
    FileUpload fileUpload;
    @UiField
    ValidationMessage messages;
    @UiField
    InputElement fileNameInSession;
    @UiField
    Button simulateAction;
    @UiField
    Button importAction;

    private String importFileNameInServerSession = null;
    private boolean simulation;

    public ImportClient() {
        initWidget(binder.createAndBindUi(this));

        simulateAction.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                simulation = true;
                submitForm();
            }
        });

        importAction.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                simulation = false;
                submitForm();
            }
        });

        fileUploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
        fileUploadForm.setMethod(FormPanel.METHOD_POST);

        fileUploadForm.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
            public void onSubmitComplete(FormPanel.SubmitCompleteEvent event) {

                ImportClientServiceAsync.Util.getInstance().readFile(simulation, importFileNameInServerSession, new IabakoAsyncCallback<String>() {

                    public void onSuccess(String result) {
                        if (result.contains("ERROR")) {
                            messages.showErrorMessage(false, false, result);
                        } else {
                            messages.showSuccessMessage(false, result);
                        }
                        super.onSuccess(result);
                    }
                });
            }
        });
    }

    private void submitForm() {
        if (fileUpload.getFilename() == null) {
            messages.showErrorMessage(false, false, "No file submitted");
        }
        importFileNameInServerSession = "importfile_" + new Date().getTime();
        fileNameInSession.setValue(importFileNameInServerSession);
        fileUploadForm.submit();
        messages.showSuccessMessage(false, "Client import in process...");
    }
}
